package com.relatev.minecraft.TRMobSpawn;

import com.relatev.minecraft.TRMobAPI.TRLevelMob;
import com.relatev.minecraft.TRMobAPI.TRMob;
import com.relatev.minecraft.TRMobAPI.TRMobAPI;
import java.util.HashMap;
import java.util.Random;
import org.bukkit.Bukkit;
import org.bukkit.Location;

public class SpawnPointTask implements Runnable {

    private static SpawnPointTask Instance;
    private static Random random;

    private HashMap<Location, Integer> PeriodCounter = new HashMap();

    public static void init() {
        random = new Random();
        Instance = new SpawnPointTask();
        Bukkit.getScheduler().runTaskTimer(TRMobSpawn.MainPlugin, Instance, 20, 20);
    }

    @Override
    public void run() {
        for (Location loc : DataManager.SpawnPoints.keySet()) {
            TRSpawnPointInfo trspi = DataManager.SpawnPoints.get(loc);
            if (PeriodCounter.containsKey(loc) == false) {
                PeriodCounter.put(loc, 0);
            }
            PeriodCounter.put(loc, PeriodCounter.get(loc) + 1);
            if (PeriodCounter.get(loc) >= trspi.SecondPeriod) {
                PeriodCounter.put(loc, 0);
                TRMob trm = TRMobAPI.getAPI().getTRMobManager().getTRMob(trspi.MobInfo.TRMobID);
                if (trm != null) {
                    if (trm instanceof TRLevelMob) {
                        ((TRLevelMob) trm).spawn(loc, trspi.MobInfo.MinLevel + random.nextInt(trspi.MobInfo.MaxLevel - trspi.MobInfo.MinLevel + 1));
                    } else {
                        trm.spawn(loc);
                    }
                }
            }
        }
    }
}
